<?php 
	jimport('joomla.application.component.model');
	class kinh_doanhModelxu_ly_phu_thu extends JModel
	{
	   function xu_ly_phu_thu($kd,$st,$gh)
       {    $db = &JFactory::getDBO();
            $user = JFactory::getUser();
            global $mainframe;
            $date = date('Y-m-d H:i:s'); // ding dang kieu ngay
            $row =  & JTable::getInstance('kinh_doanh', 'Table'); 
            $row->id_a_b = $_SESSION['id_a_b'];
            $row->date = $date;
            $row->cus_paid = $kd;
            $row->total = $st;
            $row->note = $gh;
            $row->orther = 1;
            //$row->deleted = 0;
            if (!$row->store()) { //thuc thi them record  neu co loi se hien thi
                              	     JError::raiseError(500, $row->getError() );
                                        }
             $sql = "UPDATE #__erp_agent_branch_account SET 
                                balance = balance + '".$st."'
                                WHERE 
                                id_a_b ='".$_SESSION['id_a_b']."'";
                                $db->setQuery($sql);
                                $db->query();
            if($db->errorNum !='')
                                {
                                    //xoa record vua tao
                                    $row->delete($row->id);
                                    $mainframe->redirect( "index.php?option=com_kinh_doanh", "Error","Error");
                                    }
            
            //$db=&JFactory::getDBO();
            //echo $_SESSION['id_a_b'];die;
            //$date = date('Y-m-d H:i:s');
            //$sql = "INSERT INTO #__erp_agent_branch_sale (id_a_b,date,cus_paid,total,note,orther)
              //      VALUES ('".$_SESSION['id_a_b']."','".$date."','".$kd."','".$st."','".$gh."',1)";
                //                  $db->setQuery($sql);
                  //                $rs = $db->query();  
            $mainframe->redirect( "index.php?option=com_kinh_doanh", "Success.");
       }
	}